<!DOCTYPE html>
<html>

<head>
    <title>AiSpea WiFi Config</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <style type="text/css">
        body,
        html {
            height: 100%;
            margin: 0;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            font-family: Arial, sans-serif;
            background-color: #f0f0f0;
        }

        .container {
            text-align: center;
            background-color: #ffffff;
            padding: 40px;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }

        .input {
            margin-top: 20px;
            display: flex;
            justify-content: center;
            align-items: center;
            position: relative;
        }

        .input span {
            width: 100px;
            height: 36px;
            line-height: 36px;
            text-align: right;
            margin-right: 10px;
        }

        .input input {
            height: 36px;
            width: 200px;
            padding: 0 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
            box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.1);
        }

        .dropdown {
            position: absolute;
            top: 100%;
            left: 110px;
            width: calc(100% - 110px);
            max-height: 200px;
            overflow-y: auto;
            background: white;
            border: 1px solid #ccc;
            border-radius: 5px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            z-index: 1;
            display: none;
        }

        .dropdown div {
            padding: 10px;
            cursor: pointer;
        }

        .dropdown div:hover {
            background-color: #f0f0f0;
        }

        .btn {
            width: 120px;
            height: 40px;
            background-color: #007bff;
            border: none;
            color: #ffffff;
            margin-top: 20px;
            border-radius: 5px;
            cursor: pointer;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            transition: background-color 0.3s ease;
        }

        .btn:hover {
            background-color: #0056b3;
        }

        img {
            max-width: 100%;
            height: auto;
            margin-bottom: 20px;
        }

        .footer {
            text-align: center;
            margin-top: 20px;
            font-size: 14px;
            color: #555;
        }

        .footer a {
            color: #007bff;
            text-decoration: none;
        }

        .footer a:hover {
            text-decoration: underline;
        }

        .instructions {
            font-size: 16px;
            color: #29abe2;
            margin-bottom: 20px;
        }
    </style>
</head>

<body>
<div class="container">
    <img src=""
         alt="AiSpea">
    <div class="instructions">Please enter the WiFi name and password to connect to the network:</div>
    <div class="input">
        <span>WiFi SSID:</span>
        <input type="text" id="wifi-name" placeholder="WIFI name" onfocus="showDropdown()" oninput="filterDropdown()">
        <div id="dropdown" class="dropdown"></div>
    </div>
    <div class="input">
        <span>WiFi PASS:</span>
        <input type="text" id="passWord" placeholder="WIFI password">
    </div>
    <input class="btn" type="submit" name="submit" value="submit" onclick="send_data()">
</div>

<div class="footer">
    <div>AiSpea</div>
    <div><a href="http://www.aispea.com" target="_blank">www.aispea.com</a></div>
</div>
</body>

<script>
    function send_data() {
        var filePath;
        filePath = "{\"ssid\":\"" + document.querySelector('#wifi-name').value + "\",\"passwd\":\"" + document.getElementById("passWord").value + "\"}";
        var upload_path = "/upload/" + filePath;

        var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (xhttp.readyState == 4) {
                if (xhttp.status == 200) {
                    document.open();
                    document.write(xhttp.responseText);
                    document.close();
                } else if (xhttp.status == 0) {
                    // alert("Server closed the connection abruptly!");
                    location.reload()
                } else {
                    alert(xhttp.status + " Error!\n" + xhttp.responseText);
                    location.reload()
                }
            }
        };
        xhttp.open("POST", upload_path, true);
        xhttp.send(filePath);
        alert("send done");
    }

    function showDropdown() {
        document.getElementById('dropdown').style.display = 'block';
        populateDropdown(ssidList);
    }

    function filterDropdown() {
        var input = document.getElementById('wifi-name').value.toLowerCase();
        var filteredList = ssidList.filter(function (ssid) {
            return ssid.toLowerCase().includes(input);
        });
        populateDropdown(filteredList);
    }

    function populateDropdown(list) {
        var dropdown = document.getElementById('dropdown');
        dropdown.innerHTML = '';
        list.forEach(function (item) {
            var div = document.createElement('div');
            div.textContent = item;
            div.onclick = function () {
                document.getElementById('wifi-name').value = item;
                dropdown.style.display = 'none';
            };
            dropdown.appendChild(div);
        });
    }

    window.onload = function () {
        var request = new XMLHttpRequest();

        request.open('GET', '/wifilist', true);

        request.onload = function () {
            if (request.status >= 200 && request.status < 400) {
                var data = request.responseText;
                const object = JSON.parse(data);
                ssidList = Object.keys(object);
            }
        };

        request.onerror = function () {
            // There was a connection error of some sort
        };

        request.send();
    };

    document.addEventListener('click', function (event) {
        var isClickInside = document.getElementById('wifi-name').contains(event.target) || document.getElementById('dropdown').contains(event.target);
        if (!isClickInside) {
            document.getElementById('dropdown').style.display = 'none';
        }
    });
</script>

</html>